import axios from "axios";

const request = axios.create({
    baseURL: "api/",
    timeout: 5000,
});
request.interceptors.request.use(
    (config) => {
        let token = window.localStorage.getItem("token");
        //应判断token有效
        if (token)
            config.headers.token = token;
        return config;
    }
);
request.interceptors.response.use(
    (res) => {
        return res.data;
    },
    (error) => {
        // resolve一个data对象，否则await得不到预期的data
        return Promise.resolve({code: 500, error});
    }
);

export default request;
